package com.paishen.peiniwan.module.profile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.paishen.common.dialog.LoadingDialog;
import com.paishen.common.util.BaseUtil;
import com.paishen.common.util.NetworkUtil;
import com.paishen.peiniwan.ActivityCollector;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.BusEvent;
import com.paishen.peiniwan.Once;
import com.paishen.peiniwan.R;
import com.paishen.peiniwan.database.UserDao;
import com.paishen.peiniwan.module.login.AmazingLRActivity;
import com.paishen.peiniwan.module.message.EaseBlackListActivity;
import com.paishen.peiniwan.module.set.SetAboutActivity;
import com.paishen.peiniwan.module.set.SetEditPasswordActivity;
import com.paishen.peiniwan.module.set.SetFeedbackActivity;
import com.paishen.peiniwan.module.set.SetNotificationActivity;
import com.umeng.analytics.MobclickAgent;

import cn.jpush.android.api.JPushInterface;
import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/12/1.
 */
public class MainProfileSettingsLayout extends LinearLayout {

    private TextView txCache;
    private ImageView imgCacheState;
    private View layClearCache;

    public MainProfileSettingsLayout(Context context) {
        this(context, null);
    }

    public MainProfileSettingsLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // interrupt click event
            }
        });

        LayoutInflater.from(context).inflate(R.layout.layout_main_profile_settings, this);
//        ((ViewGroup) getChildAt(0)).getChildAt(0).setPadding(0, (int) getResources().getDimension(R.dimen.title_height) + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? BaseUtil.getStatusBarHeight() : 0), 0, 0);
        setPadding(0, (int) getResources().getDimension(R.dimen.title_height) + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? BaseUtil.getStatusBarHeight() : 0), 0, 0);

        final OnClickListener onItemClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.lay_notification:
                        Intent intentNotification = new Intent(getContext(), SetNotificationActivity.class);
                        getContext().startActivity(intentNotification);
                        break;
                    case R.id.lay_blacklist:
                        Intent intentBlacklist = new Intent(getContext(), EaseBlackListActivity.class);
                        getContext().startActivity(intentBlacklist);
                        break;
                    case R.id.lay_edit_password:
                        Intent intentEditPassword = new Intent(getContext(), SetEditPasswordActivity.class);
                        getContext().startActivity(intentEditPassword);
                        break;
                    case R.id.lay_feedback:
                        Intent intentFeedback = new Intent(getContext(), SetFeedbackActivity.class);
                        getContext().startActivity(intentFeedback);
                        break;
                    case R.id.lay_about:
                        Intent intentAbout = new Intent(getContext(), SetAboutActivity.class);
                        getContext().startActivity(intentAbout);
                        break;
                    case R.id.lay_clear_cache:
                        clearCache();
                        break;
                    case R.id.view_logout:
                        final LoadingDialog dlgLoading = new LoadingDialog.Builder(getContext()).setCanCancel(false).create();
                        dlgLoading.show();

                        EMClient.getInstance().logout(true, new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                Activity act = (Activity) getContext();
                                if (!act.isFinishing()) {
                                    act.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            com.paishen.peiniwan.database.User user = App.i().getUser();
                                            user.setApp_auto_login(false);
                                            EventBus.getDefault().post(new BusEvent(BusEvent.TYPE_BACKGROUND, BusEvent.ACTION_DB_UPDATE_USER, user));
                                            // 清空登录信息
                                            MobclickAgent.onProfileSignOff();
                                            JPushInterface.stopPush(getContext().getApplicationContext());

                                            Intent intent = new Intent(getContext(), AmazingLRActivity.class);
                                            intent.putExtra(AmazingLRActivity.EXTRA_MODE_LOGIN, true);
                                            getContext().startActivity(intent);
                                            ActivityCollector.finishAllExcept(AmazingLRActivity.class);
                                        }
                                    });
                                }
                            }

                            @Override
                            public void onProgress(int progress, String status) {
                            }

                            @Override
                            public void onError(int code, String message) {
                                Activity act = (Activity) getContext();
                                if (!act.isFinishing()) {
                                    act.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            dlgLoading.dismiss();
                                            Toast.makeText(App.i(), NetworkUtil.isNoInternet() ? R.string.error_net : R.string.error_request, Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        });
                        break;
                }
            }
        };

        findViewById(R.id.lay_notification).setOnClickListener(onItemClickListener);
        findViewById(R.id.lay_blacklist).setOnClickListener(onItemClickListener);
        findViewById(R.id.lay_edit_password).setOnClickListener(onItemClickListener);
        findViewById(R.id.lay_feedback).setOnClickListener(onItemClickListener);
        findViewById(R.id.lay_about).setOnClickListener(onItemClickListener);
        layClearCache = findViewById(R.id.lay_clear_cache);
        layClearCache.setOnClickListener(onItemClickListener);
        findViewById(R.id.view_logout).setOnClickListener(onItemClickListener);

        txCache = (TextView) findViewById(R.id.tx_cache);
        imgCacheState = (ImageView) findViewById(R.id.img_cache_state);
    }

    private void clearCache() {
        layClearCache.setEnabled(false);
        txCache.setTextColor(0xFFB3B3B3);
        imgCacheState.setVisibility(View.VISIBLE);
        ((AnimationDrawable) imgCacheState.getBackground()).start();
        layClearCache.postDelayed(new Runnable() {
            @Override
            public void run() {
                layClearCache.setEnabled(true);
                txCache.setTextColor(0xFF05131A);
                imgCacheState.setVisibility(View.INVISIBLE);
                ((AnimationDrawable) imgCacheState.getBackground()).stop();
            }
        }, 2000);

        Glide.get(getContext().getApplicationContext()).clearMemory();

        Once.getInstance().clearHistory();

        EventBus.getDefault().post(new BusEvent(BusEvent.TYPE_BACKGROUND, BusEvent.ACTION_SETTINGS_CLEAR_CACHE));
    }
}
